package cn.itsource.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.domain.ArticleType;
import cn.itsource.domain.Menu;
import cn.itsource.query.MenuQuery;
import cn.itsource.service.IMenuService;
import cn.itsource.util.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(tags = "菜单接口")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    /**
     * 增加/修改
     * @param menuType
     * @return
     */
    @ApiOperation(value = "增加/修改")
    @PutMapping
    @PreAuthorize(sn="menu:add",name = "菜单增加",descs = "菜单增加")
    public R addORupdate(@RequestBody Menu menuType){
        try {
            if (menuType.getId() == null){
                menuService.add(menuType);
            }else {
                menuService.update(menuType);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn="menu:del",name = "菜单删除",descs = "菜单删除")
    public R del(@PathVariable("id") Long id){
        try {
            menuService.del(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(sn="menu:delList",name = "菜单批量删除",descs = "菜单批量删除")
    public R delList(@RequestBody List<Long> ids){
        try {
            menuService.delList(ids);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 获取单个
     * @param id
     * @return
     */
    @ApiOperation(value = "获取单个")
    @GetMapping("/{id}")
    @PreAuthorize(sn="menu:getOne",name = "菜单获取单个",descs = "菜单获取单个")
    public R getOne(@PathVariable("id") Long id){
        try {
            return R.ok(menuService.get(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(sn="menu:pageQuery",name = "菜单分页查询",descs = "菜单分页查询")
    public PageInfo<Menu> pageQuery(@RequestBody MenuQuery query){
        return menuService.pageQuery(query);
    }


    @PatchMapping("/menyTree")
    @ApiOperation(value = "获取菜单树")
    @PreAuthorize(sn="menu:menyTree",name = "菜单获取菜单树",descs = "菜单获取菜单树")
    public R<List<Menu>> articleTypeTree(){
        try {
            return R.ok(menuService.menyTree());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
}
